<template>
    <div>
        <!-- 展期申请信息:fzZqsqxx-->
        <div class="w_100 headTips d_f j_c_s_b">
            <div class="d_f a_i_c">
                <div class="line"></div>
                {{ name }}
            </div>
            <div class="isClose" @click="close">
                <el-icon v-if="type == 'smallForm'">
                    <CloseBold />
                </el-icon>
            </div>
            <el-switch v-if="type == 'bigForm'" @change="change" v-model="status"
                style="--el-switch-on-color: #13ce66;" />
                <el-button type="primary" v-if="type == 'useForm'&&isReset==1" @click="clearForm">清除表单</el-button>
        </div>
        <div class="formBox fzZqsqxx">
            <div class="fzZqsqxx_form">
                <myform :fields="fields" ref="form1" @handleSelect="handleSelect" :rules="rules" :model="model.extensionBase" :options="options"
                    class="m_t_16">
                    <!-- <template v-slot:guaranteeMeasureList>
                        <el-checkbox-group v-model="model.extensionBase.guaranteeMeasureList">
                            <el-checkbox v-for="(item, index) in options.guaranteeMeasureList" :label="item.name"
                                :value="item.id" :disabled="fields[7].disabled" />
                        </el-checkbox-group>
                    </template> -->
                </myform>
            </div>

            <div class="segment" v-if="fields1.length > 0"></div>
            <div class="formArrow d_f a_i_c m_t_30" v-if="fields1.length > 0">
                <img src="../../assets/formArrow.png" alt="">
                <span>{{ subTitle || '新加字段' }}</span>
            </div>
            <div v-if="fields1.length > 0">
                <myform :fields="fields1" :model="model.extensionBase" :options="options" class="m_t_16">
                </myform>
            </div>
        </div>
    </div>
</template>

<script lang='ts'>
import { defineComponent, reactive, toRefs, onBeforeMount, ref } from 'vue'
import { fzZqsqxxType } from './data'
import { copy } from '../../utils/date'
import { formGet, unitFormSet } from '../../api/system/flowPath'
import { listByParentArr } from '../../api/login'
import { Message } from '../../utils/resetMessage'
import { useRouter } from 'vue-router'
import { NUMBRT_REG,INTEGER} from '../../utils/regular'
import { CloseBold } from '@element-plus/icons-vue'
const model = {
    extensionBase: {
        guaMoney: '',//担保金额
        period: '',//期数
        guaranteeRate: '',//担保费率
        receivablePremium: '',//应收保费
        repaymentMode: '',//还款方式: 1-一次性还款，2-等额每月，3-等额每季，4-等额本金，5-等额本息，6-固定还款日-等额本息，7-固定还款日-最后一次还本息，8-推算日-等额本息，9-推算日还款-最后一次还本息，10-按银行借款合同执行，11-其他
        liability: '',//分责比例
        guaranteeMeasure: 1,//反担保措施 1是 2否
        // guaranteeMeasureList: [],//
        approvalRemark: '',//备注
        counterGua:1,
        isReview: 1
    },
}
export default defineComponent({
    name: '',
    props: {
        type: {
            type: String,
            default: '',
        },
        unitId: {
            type: [String, Number],
            default: '',
        },
        isEnabled: {
            type: Number,
            default: '',
        },
        nodeObj: {
            type: Object,
            default: {}
        },
        vars: {
            type: Object,
            default: {}
        },
        isAudit: {
            type: Boolean,
            default: true
        }
    },
    components: {
        CloseBold
    },
    setup(props: any) {
        const data: fzZqsqxxType = reactive({
            model: copy(model),
            status: true,
            id: '',
            name: '展期申请信息',
            subTitle: '',
            options: {
                repaymentMode: [
                    {
                        name: '一次性还款',
                        id: 1,
                    },
                    {
                        name: '等额每月',
                        id: 2,
                    },
                    {
                        name: '等额每季',
                        id: 3,
                    },
                    {
                        name: '等额本金',
                        id: 4,
                    },
                    {
                        name: '等额本息',
                        id: 5,
                    },
                    {
                        name: '固定还款日-等额本息',
                        id: 6,
                    },
                    {
                        name: '固定还款日-最后一次还本息',
                        id: 7,
                    },
                    {
                        name: '推算日还款-等额本息',
                        id: 8,
                    },
                    {
                        name: '推算日还款-最后一次还本息',
                        id: 9,
                    },
                    {
                        name: '按银行借款合同执行',
                        id: 10,
                    },
                    {
                        name: '其他',
                        id: 11,
                    },
                ],
                guaranteeMeasure: [
                    {
                        name: '是',
                        id: 1,
                    },
                    {
                        name: '否',
                        id: 2,
                    },
                ],
                guaranteeMeasureList:[
                    {
                        name:'抵押',
                        id:1
                    },
                    {
                        name:'质押',
                        id:2
                    },
                    {
                        name:'个人连带责任',
                        id:3
                    },
                    {
                        name:'第三方保证',
                        id:4
                    },
                    {
                        name:'其他',
                        id:5
                    },
                ],
                counterGua: [
                    {
                        label: '无',
                        value: 2,
                    },
                    {
                        label: '有',
                        value: 1,
                    },
                ],
            },
            fields: [
                {
                    label: "担保金额",
                    prop: 'guaMoney',
                    placeholder: '请输入担保金额',
                    type: 'input',
                    labelWidth: '150px',
                    unit: '元',
                    disabled:false,
                },
                {
                    label: "期限",
                    prop: 'period',
                    placeholder: '请输入期限',
                    type: 'number',
                    labelWidth: '150px',
                    unit: '个月',
                    disabled:false,
                },
                {
                    label: "利率",
                    prop: 'guaranteeRate',
                    placeholder: '请输入利率',
                    type: 'input',
                    labelWidth: '150px',
                    unit: '%',
                    disabled:false,
                },
                {
                    label: "应收保费",
                    prop: 'receivablePremium',
                    placeholder: '请输入应收保费',
                    type: 'input',
                    labelWidth: '150px',
                    unit: '元',
                    disabled:false,
                },
                {
                    label: "分责比例",
                    prop: 'liability',
                    placeholder: '请输入分责比例',
                    type: 'input',
                    labelWidth: '150px',
                    unit: '%',
                    disabled:false,
                },
                {
                    label: "还款方式",
                    prop: 'repaymentMode',
                    placeholder: '请选择还款方式',
                    type: 'select',
                    bindLabel: 'name',
                    bindValue: 'id',
                    labelWidth: '150px',
                    disabled:false,
                },
                // {
                //     label: "反担保措施",
                //     prop: 'guaranteeMeasure',
                //     placeholder: '请选择反担保措施',
                //     type: 'select',
                //     bindLabel: 'name',
                //     bindValue: 'id',
                //     labelWidth: '150px',
                //     disabled:false,
                // },
                // {
                //     label: "具体反担保措施",
                //     prop: 'guaranteeMeasureList',
                //     labelWidth: '150px',
                //     disabled:false,
                // },
                
                {
                    label: "具体反担保措施",
                    prop: 'guaranteeMeasure',
                    type: 'radio',
                    labelWidth: '150px',
                    disabled: false,
                },
                {
                    label: "反担保情况",
                    prop: 'approvalRemark',
                    placeholder: '请输入反担保情况',
                    type: 'textarea',
                    labelWidth: '150px',
                    disabled:false,
                },
            ],
            fields1: [],
            rules: {
                guaMoney: [
                    { required: true, message: '请输入担保金额', trigger: 'blur' },
                    { pattern: NUMBRT_REG, message: '担保金额格式不正确' }
                ],
                period: [
                    { required: true, message: '请选择期限', trigger: 'change' },
                    { pattern: INTEGER, message: '期限格式不正确' }
                ],
                guaranteeRate: [
                    { required: true, message: '请输入利率', trigger: 'blur' },
                    { pattern: NUMBRT_REG, message: '利率格式不正确' }
                ],
                receivablePremium: [
                    { required: true, message: '请输入应收保费', trigger: 'blur' },
                    { pattern: NUMBRT_REG, message: '应收保费格式不正确' }
                ],
                liability: [
                    { required: true, message: '请输入分责比例', trigger: 'blur' },
                    { pattern: NUMBRT_REG, message: '分责比例格式不正确' }
                ],
                repaymentMode: [
                    { required: true, message: '请选择还款方式', trigger: 'change' },
                ],
                guaranteeMeasure: [
                    { required: true, message: '请选择反担保措施', trigger: 'change' },
                ],
                approvalRemark: [
                    { required: true, message: '请输入反担保情况', trigger: 'blur' },
                ],
                // counterGua:[
                // { required: true, message: '请选择担保情况', trigger: 'blur' },
                // ],
            },
            isReset:2,
        })
        const router = useRouter()
        const form1 = ref()
        onBeforeMount(() => {
            methods.getData()
            data.status = props.isEnabled == 0 ? false : true
        })
        const methods = {
            close() {
                router.push('/system/flowPath/childForm/index')
            },
            //获取自定义表单
            getData() {
                formGet({ tag: 'fzZqsqxx' }).then((res: any) => {
                    data.id = res.id
                    data.name = res.name
                    data.isReset=res.isReset
                    if (!res.formStruct || res.formStruct == 'null' || res.formStruct == '{}') return
                    data.subTitle = res.subTitle
                    let formStruct = JSON.parse(res.formStruct)
                    formStruct.formFields.map((item:any)=>{
                        data.model.extensionBase[item.prop]=''
                    })
                    data.fields1 = formStruct.formFields
                    data.fields.map((item: any) => {
                        item.disabled = props.isAudit
                    })
                    data.fields1.map((item: any) => {
                        item.disabled = props.isAudit
                    })
                    if (props.vars ) {
                        const localData = props.vars
                        for (let key in data.model.extensionBase) {
                            if(localData.extensionBase[key]!=null){
                                data.model.extensionBase[key] = localData.extensionBase[key]
                            }
                        }
                        // if(props.type=='useForm'){
                        //   data.model.extensionBase.approvalRemark=''
                        // }
                    } 
                 if(!props.isAudit){
                methods.handleSelect('guaranteeMeasure')
                 }
                    let arr = formStruct.formFields.filter((item: any) => {
                        return item.type == 'select'
                    })
                    let ids = arr.map((item: any) => {
                        return item.prop
                    })
                    if (ids.length == 0) return
                    listByParentArr({ ids: ids }).then((res: any) => {
                        ids.map((item: any) => {
                            data.options[item] = res[item]
                        })
                    })
                })
            },
            //设置表单的启用跟停用
            change() {
                unitFormSet({ isEnabled: data.status ? 1 : 0, id: data.id, unitId: props.unitId }).then(() => {
                    Message({ type: 'success', message: '设置成功' })
                }).catch(() => {
                    data.status = !data.status
                })
            },
            handleSelect(prop:string){
               if(prop=='guaranteeMeasure'){
                 if(data.model.extensionBase.guaranteeMeasure=='1'){
                //  data.fields[7].disabled=false 
               }else{
                // data.fields[7].disabled=true 
            //    data.model.extensionBase.guaranteeMeasureList=[]
               }
            }
            },
            //清除表单数据
            clearForm() {
                data.model = copy(model)
},
            resultData() {
                return new Promise((resolve, reject) => {
                    form1.value.verification().then((res) => {
                        resolve(data.model)
                    }).catch(() => {
                        reject(false)
                    })
                })
            },
            saveData() {
                return new Promise((resolve, reject) => {
                    resolve(data.model)
                })
            },
        }
        return {
            ...toRefs(data),
            ...methods, form1
        }
    }
})
</script>
<style lang='scss'>
.fzZqsqxx {
    .el-form-item {
        width: 50% !important;
        margin-right: 0px !important;

        .el-form-item__content {
            width: calc(100% - 150px) !important;

            .el-input,
            .el-cascader,
            .el-select {
                width: 100% !important;
            }
        }
    }
}

.fzZqsqxx_form {
    .el-form-item:nth-child(9) {
        width: 100% !important;
    }
}
</style>